package website.haicheng.cloudauth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import website.haicheng.cloudauth.dto.RoleInfoDto;
import website.haicheng.cloudauth.entity.RoleInfo;
import website.haicheng.cloudauth.service.IRoleInfoService;
import website.haicheng.cloudauth.vo.RoleVo;
import website.haicheng.core.base.BaseQueryEntity;
import website.haicheng.core.base.Resp;
import website.haicheng.core.util.JWTUtil;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 582895699@qq.com
 * @since 2021-02-10
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/role")
public class RoleInfoController {

    @Autowired
    private IRoleInfoService roleInfoService;

    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Resp.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "角色分页")
    @GetMapping(value = "/pageRole")
    public Resp pageRole(@Validated @RequestBody BaseQueryEntity queryEntity) {
        IPage<RoleInfo> page = roleInfoService.pageRoles(queryEntity);
        return Resp.ok(page);
    }

    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = RoleInfoDto.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "添加角色")
    @PostMapping(value = "/addRole")
    public Resp addRole(@Validated @RequestBody RoleInfoDto dto, HttpServletRequest request) {
        roleInfoService.addRole(dto, JWTUtil.getAccountFromToken(request));
        return Resp.ok();
    }

    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = RoleInfoDto.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "修改角色")
    @PostMapping(value = "/updateRole")
    public Resp updateRole(@Validated @RequestBody RoleInfoDto dto, HttpServletRequest request) {
        roleInfoService.updateRole(dto, JWTUtil.getAccountFromToken(request));
        return Resp.ok();
    }

    @ApiParam(name = "id", required = true,allowEmptyValue = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Resp.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "删除角色")
    @DeleteMapping
    public Resp delRole(@NotNull(message = "[id]不可为空") Long id, HttpServletRequest request) {
        roleInfoService.delRole(id, JWTUtil.getAccountFromToken(request));
        return Resp.ok();
    }

    @ApiParam(name = "id", required = true,allowEmptyValue = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = RoleVo.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "通过角色id获取用户权限")
    @GetMapping(value = "/getRolePermission")
    public Resp getRolePermission(@NotNull(message = "[id]不可为空") Long id) {
        RoleVo roleVo = roleInfoService.getRoleAndPermission(id);
        return Resp.ok(roleVo);
    }
}
